package com.xianzhi.dao.interceptor;

import com.xianzhi.bean.BaseBean;
import com.xianzhi.util.Page;
import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.reflection.DefaultReflectorFactory;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

/**
 * Created by foreknow on 2021/2/4.
 */
@Intercepts(@Signature(type = StatementHandler.class,method = "prepare",args = {Connection.class}))
public class PageInterceptor implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
        MetaObject metaObject = MetaObject.forObject(statementHandler,
                SystemMetaObject.DEFAULT_OBJECT_FACTORY,
                SystemMetaObject.DEFAULT_OBJECT_WRAPPER_FACTORY,
                new DefaultReflectorFactory());

        MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");

        String id = mappedStatement.getId();
        if (id.endsWith("ByPage")){
            BoundSql boundSql = statementHandler.getBoundSql();
            String sql = boundSql.getSql();
            String countsql = "select count(*) from ("+sql+")t";
            Connection connection = (Connection) invocation.getArgs()[0];
            PreparedStatement preparedStatement = connection.prepareStatement(countsql);

            ParameterHandler parameterHandler = (ParameterHandler) metaObject.getValue("delegate.parameterHandler");
            parameterHandler.setParameters(preparedStatement);

            ResultSet rs = preparedStatement.executeQuery();
            BaseBean bean = (BaseBean) boundSql.getParameterObject();
            Page page = bean.getPage();
            if (rs.next()){
                page.setTotalNumber(rs.getInt(1));

            }
            String pageSql = sql+" limit "+(page.getCurrentPage()-1)*page.getPageNumber()+","+page.getPageNumber();
            //重写分页的sql
            metaObject.setValue("delegate.boundSql.sql",pageSql);

        }


        return invocation.proceed();
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target,this);
    }

    @Override
    public void setProperties(Properties properties) {

    }
}
